package com.fjbdxk.beidou.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DatabaseHelper {
    private static final String URL = "jdbc:mysql://localhost:3306/beidou";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    public static boolean insertWindTurbineData(WindTurbineData data) {
        String sql = "INSERT INTO wind_turbine_data (timestamp, plc_status, environmental_temperature, wind_speed, power, torque_percentage, generator_speed, blade_speed, wind_direction, pitch_angle, power_generation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setString(1, data.getTimestamp());
            statement.setString(2, data.getPlcStatus());
            statement.setString(3, data.getEnvironmentalTemperature());
            statement.setString(4, data.getWindSpeed());
            statement.setString(5, data.getPower());
            statement.setString(6, data.getTorquePercentage());
            statement.setString(7, data.getGeneratorSpeed());
            statement.setString(8, data.getBladeSpeed());
            statement.setString(9, data.getWindDirection());
            statement.setString(10, data.getPitchAngle());
            statement.setString(11, data.getPowerGeneration());

            int rowsAffected = statement.executeUpdate();
            connection.close();
            return rowsAffected > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {
        WindTurbineData data = new WindTurbineData("2024-06-22 07:35:00", "OK", "25°C", "10 m/s", "1000 kW", "80%", "1500 rpm", "2000 rpm", "N", "5°", "900 kW");

        boolean success = insertWindTurbineData(data);
        if (success) {
            System.out.println("数据插入成功！");
        } else {
            System.out.println("数据插入失败！");
        }
    }
}
